package com.hogan.hoganproject.base.api.protocol;

/**
 * Author: chenhu
 * Time: 2020/9/1 17:40
 * todo 特别说明
 * 这个类，可以通用所有项目，但是必须注意就是code,message,result这三个字段
 * 必须与服务器返回数据DateBean的字符串一模一样,其中SUCCESS_CODE的值就是返回码code的值
 * 泛型<T></>就是我们想要和传入的值如  Observable<HoganProtocol<List<DataBean.ResultBean>>>
 * 得到的最终是 List<DataBean.ResultBean>>
 * <p>
 * <p>
 *
 * Observable<HoganProtocol<List<DataBean.ResultBean>>> getNewData(@QueryMap Map<String, String> params);
 * 比如  /----------------------------------------------
 * {
 * "code": 200,
 * "message": "成功!",
 * "result": [{
 * "sid": "31568775",
 * "text": "追银行小姐姐全过程，看人家是怎么追女孩的。",
 * "type": "video",
 * "thumbnail": "http://wimg.spriteapp.cn/picture/2020/0903/84bbafce-ed8f-11ea-8969-1866daeb0df0_wpd.jpg",
 * "video": "http://uvideo.spriteapp.cn/video/2020/0903/84bbafce-ed8f-11ea-8969-1866daeb0df0_wpd.mp4",
 * "images": null,
 * "up": "396",
 * "down": "12",
 * "forward": "18",
 * "comment": "27",
 * "uid": "22904192",
 * "name": "百思爆笑",
 * "header": "http://wimg.spriteapp.cn/profile/20180809175405412650.jpg",
 * "top_comments_content": null,
 * "top_comments_voiceuri": null,
 * "top_comments_uid": null,
 * "top_comments_name": null,
 * "top_comments_header": null,
 * "passtime": "2020-09-17 21:10:05"
 * }, {
 * "sid": "31568566",
 * "text": "兄弟们，怎么把我的职业说得高端一些？",
 * "type": "video",
 * "thumbnail": "http://wimg.spriteapp.cn/picture/2020/0902/d39a6c04-ecc5-11ea-ac6c-1866daeb0df0_wpd.jpg",
 * "video": "http://uvideo.spriteapp.cn/video/2020/0902/d39a6c04-ecc5-11ea-ac6c-1866daeb0df0_wpd.mp4",
 * "images": null,
 * "up": "277",
 * "down": "3",
 * "forward": "10",
 * "comment": "27",
 * "uid": "20620899",
 * "name": "遇一狗白首",
 * "header": "http://wimg.spriteapp.cn/profile/20190318103732351696.jpeg",
 * "top_comments_content": "神农",
 * "top_comments_voiceuri": "",
 * "top_comments_uid": "22956417",
 * "top_comments_name": "周正洋",
 * "top_comments_header": "http://wimg.spriteapp.cn/profile",
 * "passtime": "2020-09-17 21:00:05"
 * }]
 * }
 ***/

public class HoganProtocol<T> implements BaseProtocol<T> {
    private final static int SUCCESS_CODE = 200;
    private int code;
    private String message;
    private T result;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    @Override
    public Object startProtocol() {
        return handleResult(getCode(), getMessage());
    }

    private Object handleResult(int code, String msg) {
        if (SUCCESS_CODE == code) {
            return getResult();
        } else {
            return new ServerApiException(code, msg);
        }
    }
}
